# -*- coding: utf-8 -*-

import random


class TaskManager:
    """ TaskManager provides a method execution class. All the task created in
    the nodes would be added to the list managed inside the class. It runs all
    the tasks in order or shuffle the order and then to execute.
    """
    def __init__(self, shuffle: bool = False):
        """ Create a instance of the task manager.
        Args:
            shuffle: Set to true to enable shuffle execution.
        """
        # Save the passing in task list.
        self.__task_list = []
        # Set the shuffle option.
        self.__shuffle = shuffle

    def set_task_list(self, task_list: list) -> None:
        # Directly save the task list.
        self.__task_list = task_list

    def run(self) -> None:
        # Check the shuffle option is on.
        if self.__shuffle:
            # Shuffle the list.
            random.shuffle(self.__task_list)
        # Run all the tasks for one step.
        for task_instance in self.__task_list:
            # Execute the task.
            task_instance()

    def run_steps(self, n: int) -> None:
        # Loop for multiple times.
        for _ in range(n):
            # Loop and run for multiple times.
            self.run()